package com.jwdf.uda.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.ViewTicketHeaderBinding;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.GuideGetTicketsAction;
import com.jwdf.uda.ui.adapter.TicketsAdapter;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.ui.fragment.base.SimpleListFragment;

import retrofit2.Response;

/**
 * Created by Administrator on 2016/6/25.
 */
public class UserTicketActivity extends AbsTitleBarActivity {

    public static void start(Context context, int count) {
        Intent intent = new Intent(context, UserTicketActivity.class);
        intent.putExtra("tickets", count);
        context.startActivity(intent);
    }

    @Override
    protected String getTopTitle() {
        return getString(R.string.user_coin);
    }

    @Override
    protected BaseFragment onCreateContentFragment() {
        return new TicketFragment();
    }

    @Override
    protected Bundle onPutArguments() {
        Bundle args = new Bundle();
        args.putInt("tickets", getIntent().getIntExtra("tickets", 0));
        return args;
    }

    public static class TicketFragment extends SimpleListFragment {
        private TicketsAdapter mAdapter;
        @Override
        protected boolean canPullRefresh() {
            return false;
        }
        @Override
        protected void initViews() {
            ViewTicketHeaderBinding headerBinding=DataBindingUtil.inflate(getActivity().getLayoutInflater(),R.layout.view_ticket_header,mDataBinding.commonFragmentList,false);
            mDataBinding.commonFragmentList.setPullEnable(false);
            headerBinding.tvTicketCout.setText(""+getArguments().getInt("tickets",0));
            mDataBinding.commonFragmentList.addHeaderView(headerBinding.getRoot());
            mAdapter=new TicketsAdapter(getActivity());
            mDataBinding.commonFragmentList.setAdapter(mAdapter);
        }
        @Override
        protected void onListItemClick(int position) {

        }
        @Override
        protected void requestData(final boolean refresh) {
            if (refresh && mAdapter.isEmpty()) {
                showProgressDialog();
            }
            int offset = refresh ? 0 : mAdapter.getCount();
            GuideGetTicketsAction action =GuideGetTicketsAction.newInstance(getContext(),offset);
            onNewRequestCall(action.request(new AHttpService.IResCallback<PbBean.PbResGuideGetGuideTicketList>() {
                @Override
                public void onCallback(int resultCode, Response<PbBean.PbResGuideGetGuideTicketList> response, PbBean.PbBaseServerInfo baseServerInfo) {
                    if (getView() != null) {
                        closeProgressDialog();
                        if (HttpHelper.showError(getContext(), resultCode, baseServerInfo, getString(R.string.error_net))) {
                            mDataBinding.commonFragmentList.stopRefresh();
                            mDataBinding.commonFragmentList.stopLoadMore();
                            return;
                        }
                        doSuccessResponse(refresh, response.body());
                    }
                }
            }));
        }
        private void doSuccessResponse(boolean refresh, PbBean.PbResGuideGetGuideTicketList body) {
            if (body != null) {
                if (refresh) {
                    mAdapter.setData(body.getTicketListList());
                } else {
                    mAdapter.append(body.getTicketListList());
                }
                mDataBinding.commonFragmentList.setPullLoadEnable(body.getTicketListList().size() == getPageLength());
                mDataBinding.commonFragmentList.stopRefresh();
                mDataBinding.commonFragmentList.stopLoadMore();
            }
        }
    }
}
